package com.itheima.ai.repository;

import groovy.util.logging.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

@Slf4j
@Component
public class ChatRepositoryInMemory implements ChatRepository {
    private static final Logger log = LoggerFactory.getLogger(ChatRepositoryInMemory.class);
    private final Map<String, Set<String>> chatIds = new HashMap<>();

    @Override
    public void addChatId(String type, String chatId) {
        Set<String> ids = chatIds.computeIfAbsent(type, k -> new TreeSet<>()); //TODO computeIfAbsent()好方法
        ids.add(chatId);
        log.info("add history, type: {}, chatId: {}", type, chatId);
    }

    @Override
    public Set<String> getChatIds(String type) {
        return chatIds.getOrDefault(type, new TreeSet<>()); //TODO getOrDefault()好方法
    }
}
